package com.techsen.tsweb.sys.web.controller;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.techsen.tsweb.core.domain.JsonResult;
import com.techsen.tsweb.core.util.TreeUtil;
import com.techsen.tsweb.core.util.ValidUtil;
import com.techsen.tsweb.sys.domain.Org;
import com.techsen.tsweb.sys.domain.OrgRule;
import com.techsen.tsweb.sys.service.OrgRuleService;
import com.techsen.tsweb.sys.service.OrgService;

@Controller
@RequestMapping("/sys/org")
public class OrgController {

    @Resource
    private OrgService orgService;
    
    @Resource
    private OrgRuleService orgRuleService;
    
    /**
     * 进入组织管理界面
     */
    @RequestMapping
    public String index(String menuId, Model model) {
        model.addAttribute("menuId", menuId);
        return "sys/org";
    }
    
    /**
     * 根据组织类型集合查询父级组织集合
     */
    @RequestMapping("/list")
    public @ResponseBody List<Org> listAvailableParentsByType(boolean tree, 
            @RequestParam(value = "orgTypes[]", required = false) List<String> orgTypes) {
        List<Org> orgs = new ArrayList<Org>();
        if (ValidUtil.isValid(orgTypes)) {
            orgs = this.orgService.findByOrgTypes(orgTypes);
        } else {
            orgs = this.orgService.findAll();
        }
        if (tree) {
            orgs = TreeUtil.toTree(orgs);
        }
        return orgs;
    }

    /**
     * 根据组织类型查询父级组织集合
     */
    @RequestMapping("/list-available-parents-by-type")
    public @ResponseBody List<Org> listAvailableParentsByType(boolean tree, String orgType) {
        List<Org> orgs = new ArrayList<Org>();
        if (ValidUtil.isValid(orgType)) {
            List<OrgRule> orgRules = this.orgRuleService.getListByEntity(new OrgRule().setChildOrgType(orgType));
            if (ValidUtil.isValid(orgRules)) {
                List<String> parentOrgTypes = new ArrayList<String>(orgRules.size());
                for (OrgRule orgRule : orgRules) {
                    parentOrgTypes.add(orgRule.getParentOrgType());
                }
                orgs = this.orgService.findByOrgTypes(parentOrgTypes);
            }
        } else {
            orgs = this.orgService.findAll();
        }
        if (tree) {
            orgs = TreeUtil.toTree(orgs);
        }
        return orgs;
    }
    
    /**
     * 根据组织类型列出所有父组织类型的所有组织
     */
    @RequestMapping("/list-available-parents-by-id")
    public @ResponseBody List<Org> listAvailableParentsById(boolean tree, String orgId) {
        List<Org> orgs = new ArrayList<Org>();
        Org childOrg = this.orgService.get(orgId);
        if (childOrg != null) {
            List<OrgRule> orgRules = this.orgRuleService.getListByEntity(new OrgRule().setChildOrgType(childOrg.getOrgType()));
            if (ValidUtil.isValid(orgRules)) {
                List<String> orgTypes = new ArrayList<String>(orgRules.size());
                for (OrgRule orgRule : orgRules) {
                    orgTypes.add(orgRule.getParentOrgType());
                }
                orgs = this.orgService.findByOrgTypes(orgTypes);
                List<String> childrenOrgIds = this.orgService.getAllChildrenOrgId(orgId);
                if (ValidUtil.isValid(childrenOrgIds)) {
                    Iterator<Org> iter = orgs.iterator();
                    while (iter.hasNext()) {
                        if (childrenOrgIds.contains(iter.next().getId())) {
                            iter.remove();
                        }
                    }
                }
            }
        }
        if (tree) {
            orgs = TreeUtil.toTree(orgs);
        }
        return orgs;
    }
    
    /**
     * 根据组织类型，组织ID查看相应的组织对象
     */
    @RequestMapping("/{orgType}/{orgId}")
    public String view(@PathVariable("orgType") String orgType,
            @PathVariable("orgId") String orgId, String menuId) {
        return "redirect:/sys/" + orgType + "/orgid/" + orgId;
    }
    
    /**
     * 根据组织ID查询所有子组织集合
     */
    @RequestMapping("/children/{id}")
    public @ResponseBody List<Org> children(boolean tree,
            @PathVariable("id") String id) {
        List<Org> children = this.orgService.getAllChildrenOrg(id);
        if (tree) {
            children = TreeUtil.toTree(children);
        }
        return children;
    }
    
    /**
     * 进入添加组织界面
     */
    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public String add(String parentType, String parentId, Model model) {
        model.addAttribute("parentType", parentType);
        model.addAttribute("parentId", parentId);
        return "sys/org-add";
    }
    
    /**
     * 添加组织
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public @ResponseBody JsonResult add(Org org) {
        this.orgService.add(org);
        return new JsonResult(org);
    }
    
    /**
     * 删除组织
     */
    @RequestMapping("/delete")
    public @ResponseBody JsonResult delete(String id) {
        if (ValidUtil.isValid(id)) {
            this.orgService.delete(id);
        }
        return new JsonResult(id);
    }
    
    /**
     * 进入修改组织界面
     */
    @RequestMapping(value = "/update", method = RequestMethod.GET)
    public String update(String orgId, Model model) {
        Org org = this.orgService.get(orgId);
        model.addAttribute("org", org);
        return "sys/org-update";
    }
    
    /**
     * 修改组织
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public @ResponseBody JsonResult update(Org org) {
        this.orgService.update(org);
        return new JsonResult(org);
    }
    
}
